﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using EasyToHelp.DBFactory;
using EasyToHelp.Entity;
using Newtonsoft.Json;
using EasyToHelp.Common;

namespace EasyToHelp.Business
{
    public static class GridBusiness
    {
        #region Grid数据源
        /// <summary>
        /// Grid数据源
        /// </summary>
        /// <param name="Table"></param>
        /// <param name="WhereJson"></param>
        /// <returns></returns>
        public static DataTable GetTableInfo(string Table, string WhereJson)
        {
            try
            {
                string sql = string.Empty;
                if (!string.IsNullOrWhiteSpace(Table))
                {
                    sql = string.Format("SELECT * FROM {0}", Table);
                }
                StringBuilder WhereSql = new StringBuilder();
                if (!string.IsNullOrWhiteSpace(WhereJson))
                {

                }
                return MySqlHelper.GetDataTable(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region Grid列表信息
        public static List<GridManageField> GetGridManageFieldList(string TableName)
        {
            string sql = string.Format("SELECT 	*  FROM GridManageField WHERE CodeName='{0}'", TableName);
            return MySqlHelper.GetList<GridManageField>(sql);
        }
        #endregion

        #region 获取GetGridData数据源
        /// <summary>
        /// 获取GetGridData数据源
        /// </summary>
        /// <param name="Table"></param>
        /// <param name="WhereJson"></param>
        /// <returns></returns>
        public static object GetGridData(string Table, string WhereJson)
        {
            return JsonConvert.DeserializeObject(HttpHelper.HttpGet(string.Format(APIConfig.GridData, Table, WhereJson)));
        }
        #endregion

        #region 同步数据操作
        public static object synTableManageField(string TableName)
        {
            if (GetGridManageFieldList(TableName).Count > 0)
            {
                return ResultInfo.Error("非常遗憾，同步失败！原因：您已经添加过了！");
            }
            else
            {
                var Syscolumns = GetTableInfo(TableName);
                int index = 0;
                string Sql;
                try
                {
                    foreach (var item in Syscolumns)
                    {
                        Sql = string.Format(@"INSERT INTO gridmanagefield(
	                                        ChineseName,EnglishName,FieldType,CodeName,BusinessName,IsPkName,IsFkName,IsTitle,IsSearchPkName,IsFixed,IsSort,SortOrder
	                                        )VALUES(
	                                        '{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}'
	                                        )", item.column, item.column, item.type, TableName, TableName, 0, 0, 0, 0, 0, 0, ++index);
                        MySqlHelper.Execute(Sql);
                    }
                }
                catch (Exception ex)
                {
                    return ResultInfo.Fail("系统错误：" + ex.ToString());
                }
                return ResultInfo.Success("您贼幸运，已同步成功【" + (index) + "条数据】！");
            }
        }
        #endregion


        #region 获取表信息
        /// <summary>
        /// 获取表信息
        /// </summary>
        /// <param name="TableName"></param>
        /// <returns></returns>
        public static List<TableInfo> GetTableInfo(string TableName)
        {
            var sqlSyscolumns = string.Format(" SELECT  COLUMN_NAME as 'column' ,DATA_TYPE as 'type' ,COLUMN_TYPE as 'length' FROM information_schema.`COLUMNS` where TABLE_NAME like '{0}'", TableName);
            return MySqlHelper.GetList<TableInfo>(sqlSyscolumns);
        }
        #endregion
    }
}
